<?php
namespace catchAdmin\newsDeclare\model\search;
use catchAdmin\externalGoods\model\Category;
use catchAdmin\externalGoods\service\CategoryService;
use catcher\CatchAuth;
use think\facade\Db;

trait NewsDeclareSearch
{
//    public function searchArbitraryFieldAttr($query, $value, $data){
//        return $query->where('g.brand_name|g.goods_name|goods_keywords','like',"%$value%");
//    }

    //是否添加
    public function searchIsAddedAttr($query, $value, $data){
        $user = (new CatchAuth())->user();
        $tenant_id = $user['tenant_id'];
        $user_id = $user['id'];
        //$_v = $value == '已选' ? 'not null' : 'null';

        if($value == '已选'){
            $_v = 'not null';
            return
                $query->field("IF(judge_id IS NULL, '未选', '已选') as is_rec")
                    ->leftJoin('db_news_review nr',"nr.article_id =nd.id and nr.judge_id = {$user_id}  and nr.deleted_at = 0")
                    ->where('nr.article_id',$_v);
        }elseif($value == '未选'){
            $_v = 'null';
            return
                $query->field("IF(judge_id IS NULL, '未选', '已选') as is_rec")
                      ->leftJoin('db_news_review nr',"nr.article_id =nd.id and nr.judge_id = {$user_id}  and nr.deleted_at = 0")
                      ->where('nr.article_id',$_v);
        }else{
            //$_v = 'null';
            return
                  $query->field("IF(judge_id IS NULL, '未选', '已选') as is_rec")
                        ->leftJoin('db_news_review nr',"nr.article_id =nd.id and nr.judge_id = {$user_id}  and nr.deleted_at = 0");
//                    ->where('nr.article_id',$_v);
        }

//        return
//                $query->leftJoin('db_news_review nr',"nr.article_id =nd.id and nr.tenant_id = {$tenant_id}  and nr.deleted_at = 0")
//                    ->where('nr.article_id',$_v);
//            ->where('tg.tenant_id',$tenant_id)
            ;

        //return $query->where('is_sale', $value);
    }

    public function searchTypeAttr($query, $value, $data){
        return $query->where('nd.type',$value);
    }

    public function searchType1Attr($query, $value, $data){
        return $query->where('nd.type_1',$value);
    }

    //品牌名
    public function searchBrandNameAttr($query, $value, $data){
        //return $query->where('brand_name','like',"%$value%");
        return $query->whereLike('sg.brand_name', $value);
    }

    //商品名
    public function searchGoodsNameAttr($query, $value, $data){
        return $query->whereLike('sg.goods_name', $value);
    }

    //贸易类型
    public function searchTradeTypeNameAttr($query, $value, $data){
        return $query->where('sg.trade_type_name', $value);
    }

    //商品分类
    public function searchCategoryIdAttr($query, $value, $data)
    {
        $categoryModel = new Category();
        $h_id = $categoryModel->where('id',$value)->value('cate_id');

        //取出所有下级分类
        $c_id = explode(',', (new CategoryService())->get_all_child_cate($h_id));
        $c_id[] = $h_id;

        $c_id = array_values(array_filter($c_id));

        return $query->where('sg.category_id','in', $c_id);
    }
}